/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.resrev.web.page.component;

import com.rafkind.resrev.dao.ResourceDao;
import com.rafkind.resrev.data.Resource;
import com.rafkind.resrev.web.page.EditResource;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Fictional Hero
 */
public class EditOrDeleteResourcePanel extends Panel {

  @SpringBean
  private ResourceDao resourceDao;
  
  public EditOrDeleteResourcePanel(String id, IModel<Resource> model) {
    super(id, model);
    
    Link editLink = new Link("editLink"){
      @Override
      public void onClick() {
        setResponsePage(
            new EditResource(
              new Model(
                (Resource) EditOrDeleteResourcePanel.this.getDefaultModelObject()), 
                getPage()));
      }      
    };
    Link deleteLink = new Link("deleteLink"){
      @Override
      public void onClick() {
        resourceDao.deleteResource((Resource) EditOrDeleteResourcePanel.this.getDefaultModelObject());
      }      
    };
    deleteLink.add(
        new AttributeAppender(
          "onclick",
          new Model("return confirm('Really delete resource " 
            + model.getObject().getName() + "?');"),
          ";"));
    
    add(editLink);
    add(deleteLink);
  }  
}